package com.dx.mes.service.query.service;

import com.dx.common.table.ppt.Bis_data;
import com.dx.common.table.ppt.Ret_prd_jge;
import com.dx.mes.common.dao.IBisDataDao;
import com.dx.mes.common.dao.IRetPrdJgeDao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.query.service.IMipshtjudgeService;
import com.dx.mes.facade.query.tx.Mipshtjudge.MipshtjudgeI;
import com.dx.mes.facade.query.tx.Mipshtjudge.MipshtjudgeO;
import com.dx.mes.facade.query.tx.Mipshtjudge.MipshtjudgeOA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

import static com.dx.mes.common.bean.def.GenericDef.*;

/**
 * Created by admin on 2017/12/27.
 */
@Service("mipshtjudgeService")
public class MipshtjudgeService implements IMipshtjudgeService {
    private LogUtils logUtils;
    private static final String TX_NAME = "MIPSHTJUDGE";
    @Autowired
    private IRetPrdJgeDao retPrdJgeDao;
    @Autowired
    private IBisDataDao bisDataDao;

    @Override
    public String subMainProc(String evt_no, String strInTrx) {
        AppContext.clear();
        AppContext.setCurrServiceName(MipwobomService.class.getSimpleName());
        AppContext.setCurrEventNumber(evt_no);
        logUtils = new LogUtils(MipshtjudgeService.class);
        logUtils.info("[InTrx:" + strInTrx + "]");
        String outMsg = "";

        MipshtjudgeI inTrx = new MipshtjudgeI();
        MipshtjudgeO outTrx = new MipshtjudgeO();

        outTrx.setTrx_id(TX_NAME);
        outTrx.setType_id(TRX_OUT);
        outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

        try {
            inTrx = JacksonUtil.fromJson(strInTrx, MipshtjudgeI.class);

            subMainProc2(inTrx.getAction_flg().toCharArray()[0],inTrx,outTrx);
            if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
                retPrdJgeDao.flushCurSession();
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } catch (Exception ex) {
            outTrx.setRtn_mesg(ex.toString());
            outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
            logUtils.error(Util.stackTraceToString(ex));
            TransactionAspectSupport.currentTransactionStatus()
                    .setRollbackOnly();
        } finally {
            outMsg = JacksonUtil.toJSONStr(outTrx);
            logUtils.info("[OutTrx:" + outMsg + "]");
        }
        return outMsg;
    }
    private void subMainProc2(char actionType, MipshtjudgeI inTrx,   MipshtjudgeO outTrx) {

        switch (actionType) {
            case 'Q':{
                getJudByCondition(inTrx, outTrx);
                break;
            }
            default: {
                String gszMsgBuf = "Invalide type input: [" + actionType + "]";
                outTrx.setRtn_mesg(gszMsgBuf);
                outTrx.setRtn_code(String.valueOf(E_FIPINQBOX_INVALID_INPUT));
            }
        }
    }
    private void getJudByCondition(MipshtjudgeI inTrx, MipshtjudgeO outTrx) {

        String  prdSeqId  = inTrx.getPrd_seq_id();
        List<MipshtjudgeOA> oary_jud = new ArrayList<>();
        String hql = "From Ret_prd_jge Where prd_seq_id_fk='" +prdSeqId+ "'";
        List<Ret_prd_jge>  ret_prd_jges  = retPrdJgeDao.find(hql);
        for(Ret_prd_jge ret_prd_jge : ret_prd_jges){
            MipshtjudgeOA oary = new MipshtjudgeOA();
            if(!Util.isSpaceCheck(ret_prd_jge.getDef_code())){
                oary.setDef_code(ret_prd_jge.getDef_code());
                String sql ="From Bis_data where data_cate='DFCT' and data_id ='" +ret_prd_jge.getDef_code()+ "'";
                List<Bis_data> datas = bisDataDao.find(sql);
                if(datas.size()>0){
                    oary.setDef_code_desc(datas.get(0).getData_ext());
                }
                oary_jud.add(oary);
            }
        }
        outTrx.setOaryA(oary_jud);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }
}
